package com.alibaba.citrus.ots.unified.inventory.repository.impl;

import com.alibaba.citrus.ots.unified.inventory.repository.ConsignmentOrderRepository;
import com.alibaba.citrus.ots.unified.inventory.utils.ConsignmentConverter;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsunifiedinventory.consignmentorder.dto.CreateConsignmentOrderResponse;
import com.epoch.app.otsunifiedinventory.consignmentorder.dto.DeleteConsignmentOrderRequest;
import com.epoch.app.otsunifiedinventory.consignmentorder.model.ConsignmentOrder;
import com.epoch.app.otsunifiedinventory.consignmentorder.service.ConsignmentOrderService;
import com.epoch.app.otsunifiedinventory.consignmentorderline.dto.DeleteConsignmentOrderLineRequest;
import com.epoch.app.otsunifiedinventory.consignmentorderline.model.ConsignmentOrderLine;
import com.epoch.app.otsunifiedinventory.consignmentorderline.service.ConsignmentOrderLineService;
import com.epoch.app.otsunifiedinventory.model.dto.ConsignmentOrderLineLoadListRequest;
import com.epoch.app.otsunifiedinventory.model.dto.ConsignmentOrderLoadListRequest;
import com.epoch.app.otsunifiedinventory.sdo.ConsignmentOrderLineSDO;
import com.epoch.app.otsunifiedinventory.sdo.ConsignmentOrderSDO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ConsignmentOrderForDiffRepositoryImpl implements ConsignmentOrderRepository {

    @Resource
    ConsignmentOrderService consignmentOrderService;

    @Resource
    ConsignmentOrderLineService consignmentOrderLineService;

    @Override
    public String create(ConsignmentOrderSDO consignmentOrderSDO) {
        ConsignmentOrder consignmentOrder = ConsignmentConverter.converterOrder(consignmentOrderSDO);
        CreateConsignmentOrderResponse consignmentOrder1 = consignmentOrderService.createConsignmentOrder(consignmentOrder);
        return consignmentOrder1.getLastInsertId();
    }

    @Override
    public Boolean delete(ConsignmentOrderSDO consignmentOrderSDO) {
        ConsignmentOrder consignmentOrder = ConsignmentConverter.converterOrder(consignmentOrderSDO);
        consignmentOrderService.updateConsignmentOrder(consignmentOrder);
        return true;
    }

    @Override
    public ConsignmentOrderSDO query(String outerDeliveryId) {
        ConsignmentOrderLoadListRequest request = new ConsignmentOrderLoadListRequest();
        request.setOuterDeliveryOrderId(outerDeliveryId);
        List<ConsignmentOrder> result = consignmentOrderService.loadConsignmentOrderList(request).getResult();
        if (CollectionUtils.isNotEmpty(result)) {
            ConsignmentOrder order = result.get(0);
            ConsignmentOrderSDO consignmentOrderSDO = new ConsignmentOrderSDO();
            consignmentOrderSDO.setId(order.getId().toString());
            consignmentOrderSDO.setOuterDeliveryOrderId(order.getOuterDeliveryOrderId());
            consignmentOrderSDO.setOuterSalesOrderId(order.getOuterSalesOrderId());
            consignmentOrderSDO.setOuterStoOrderId(order.getOuterStoOrderId());
            consignmentOrderSDO.setSalesOrderId(order.getSalesOrderId());
            consignmentOrderSDO.setStoOrderId(order.getStoOrderId());
            consignmentOrderSDO.setSalesOrgId(order.getSalesOrgId());
            consignmentOrderSDO.setCreator(order.getCreator());
            consignmentOrderSDO.setCreatDate(order.getGmt_create());
            consignmentOrderSDO.setModifier(order.getUpdater());
            consignmentOrderSDO.setModifidDate(order.getGmt_modified());
            consignmentOrderSDO.setDeliveryDate(order.getDeliveryDate());
            consignmentOrderSDO.setFeatures(order.getFeatures());
            consignmentOrderSDO.setStatus(order.getStatus());
            consignmentOrderSDO.setSyncVersion(order.getSyncVersion());

            ConsignmentOrderLineLoadListRequest lineLoadListRequest = new ConsignmentOrderLineLoadListRequest();
            lineLoadListRequest.setConsignmentOrderId(order.getId().toString());
            lineLoadListRequest.setStart(0);
            lineLoadListRequest.setLimit(999);
            Result<List<ConsignmentOrderLine>> listResult = consignmentOrderLineService.loadConsignmentOrderLineList(lineLoadListRequest);
            if (CollectionUtils.isNotEmpty(listResult.getResult())) {
                List<ConsignmentOrderLineSDO> collect = listResult.getResult().stream().map(line -> {
                    ConsignmentOrderLineSDO orderLineSDO = new ConsignmentOrderLineSDO();
                    orderLineSDO.setId(line.getId().toString());
                    orderLineSDO.setOuterOrderLineId(line.getOuterOrderLineId());
                    orderLineSDO.setOuterSoLineId(line.getOuterSoLineId());
                    orderLineSDO.setSoLineId(line.getOuterSoLineId());
                    orderLineSDO.setOuterStoLineId(line.getOuterStoLineId());
                    orderLineSDO.setStoLineId(line.getStoLineId());
                    orderLineSDO.setScItemId(line.getScItemId());
                    orderLineSDO.setQty(line.getQty());
                    orderLineSDO.setPodStatus(line.getPodStatus());
                    orderLineSDO.setPodQty(line.getPodQty());
                    orderLineSDO.setFactoryCode(line.getFactoryCode());
                    orderLineSDO.setInventoryCode(line.getInventoryCode());
                    orderLineSDO.setCreator(line.getCreator());
                    orderLineSDO.setCreateDate(line.getGmt_create());
                    orderLineSDO.setModifier(line.getUpdater());
                    orderLineSDO.setModifidDate(line.getGmt_modified());
                    orderLineSDO.setStatus(line.getStatus());
                    orderLineSDO.setSyncVersion(line.getSyncVersion());
                    orderLineSDO.setConsignmentOrderId(line.getConsignmentOrderId());
                    return orderLineSDO;
                }).collect(Collectors.toList());
                consignmentOrderSDO.setConsignmentOrderLineSDOS(collect);
            }
            return consignmentOrderSDO;
        }
        return null;
    }

    @Override
    public void deleteOrder(ConsignmentOrderSDO consignmentOrderSDO) {
        DeleteConsignmentOrderRequest deleteConsignmentOrderRequest = new DeleteConsignmentOrderRequest();
        deleteConsignmentOrderRequest.setId(Long.parseLong(consignmentOrderSDO.getId()));
        consignmentOrderService.deleteConsignmentOrder(deleteConsignmentOrderRequest);
        for (ConsignmentOrderLineSDO consignmentOrderLineSDO : consignmentOrderSDO.getConsignmentOrderLineSDOS()) {
            DeleteConsignmentOrderLineRequest request = new DeleteConsignmentOrderLineRequest();
            request.setId(Long.parseLong(consignmentOrderLineSDO.getId()));
            consignmentOrderLineService.deleteConsignmentOrderLine(request);
        }
    }
}
